/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.help.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.help.model.HelpFolder;
import org.crystalrange.help.service.HelpFolderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Window;

public class HelpFolderController extends BaseController {

    protected HelpFolderService helpFolderService;
    protected HelpFolder helpFolder;

    public HelpFolderService getHelpFolderService() {
        return this.helpFolderService;
    }

    public void setHelpFolderService(HelpFolderService helpFolderService) {
        this.helpFolderService = helpFolderService;
    }

    public HelpFolder getHelpFolder() {
        return this.helpFolder;
    }

    public void setHelpFolder(HelpFolder helpFolder) {
        this.helpFolder = helpFolder;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.helpFolder = this.helpFolderService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Help Folder");
        } else {
            this.helpFolder = this.helpFolderService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Help Folder");
        }

        component.setAttribute("controller", this, true);
    }

    public void onOpen$bandboxParentHelpFolder(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/helpFolderSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            HelpFolder selectedHelpFolder = (HelpFolder) this.execution.getAttribute("selectedHelpFolder");
            this.helpFolder.setParentHelpFolder(selectedHelpFolder);
        }

        this.binder.loadAll();
    }

    public void onClick$menuitemHelpFolderSave(Event event) throws Exception {
        this.helpFolderSave();
    }

    public void onClick$toolbarbuttonHelpFolderSave(Event event) throws Exception {
        this.helpFolderSave();
    }

    private void helpFolderSave() throws Exception {
        this.helpFolderService.save(this.helpFolder,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
